<!--
  Copyright JS Foundation and other contributors, http://js.foundation

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<script type="text/x-red" data-template-name="config-manager">
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> <span>Name</span></label>
        <input type="text" id="node-input-name" disabled>
    </div>
	<div class="form-row" style="margin-bottom:0;">
        <label><i class="fa fa-list"></i> Config</span></label>
    </div>
    <div class="form-row node-input-rule-container-row">
        <ol id="node-input-rule-container"></ol>
    </div>
</script>

<script type="text/x-red" data-help-name="config-manager">
    <p>A node to route messages based on property values.</p>
    <p>When a message arrives, the selected property is evaluated against each
    of the defined rules. The message is then sent to the output of <i>all</i>
    rules that pass.</p>
    <p><b>Note</b>: the <i>otherwise</i> rule applies as a "not any of" the rules preceding it.</p>
</script>

<script type="text/javascript">
    RED.nodes.registerType('config-manager', {
        color: "#E2D96E",
        category: 'input',
        defaults: {
            name: {value:"config manager"},
            configs: {value:[{}]},
            defaultConfigs: {value:[{}]}
        },
        inputs: 0,
        outputs: 0,
        icon: "file.png",
        label: function() {
            return this.name||"config manager";
        },
        oneditprepare: function() {
            var node = this;
            var previousValueType = {value:"prev",label:this._("inject.previous"),hasValue:false};

            node.defaultConfigs = node.defaultConfigs || [{cn:"relayinitinfo",cp:"gw/config/relayinitinfo.json"},{cn:"message",cp:"gw/config/message.json"},{cn:"ecoserver",cp:"gw/config/ecoserver.json"},{cn:"controller",cp:"gw/config/controller.json"}];

            function resizeRule(rule) {
                var newWidth = rule.width();
                var nameField = rule.find(".node-input-name-value");
                var pathField = rule.find(".node-input-path-value");
                pathField.width(newWidth-nameField.width()-70);
            }

            $("#node-input-rule-container").css('min-height','250px').css('min-width','450px').editableList({
                addItem: function(container,i,opt) {
                    if (!opt.hasOwnProperty('r')) {
                        opt.r = {};
                    }
                    var configs = opt.r;
                    var row = $('<div/>', {class:"form-row"}).appendTo(container);
                    var configNameField;
                    var configPathField;
					if (opt.d) {
						configNameField = $('<input/>',{class:"node-input-name-value",type:"text", style:"width:120px; margin-left: 5px;", disabled:"disabled"}).appendTo(row);
						configPathField = $('<input/>',{class:"node-input-path-value",type:"text",style:"width:238px;margin-left: 5px;",disabled:"disabled"}).appendTo(row);

					} else {
					  	configNameField = $('<input/>',{class:"node-input-name-value",type:"text", style:"width:120px; margin-left: 5px;"}).appendTo(row);
            			configPathField = $('<input/>',{class:"node-input-path-value",type:"text",style:"width:238px;margin-left: 5px;"}).appendTo(row);
					}

                    configNameField.val(configs.cn);
                    configPathField.val(configs.cp);
                },
                resizeItem: resizeRule,
                sortable: true,
                removable:true
            });
            for (var i=0;i<node.defaultConfigs.length;i++) {
              	var config = node.defaultConfigs[i];
              	$("#node-input-rule-container").editableList('addItem',{r:config,i:i,d:true});
         	}
            for (var i=0;i<node.configs.length;i++) {
                var config = node.configs[i];
                $("#node-input-rule-container").editableList('addItem',{r:config,i:i,d:false});
            }
        },
        oneditsave: function() {
            var configs = $("#node-input-rule-container").editableList('items');
            var node = this;
            node.configs = [];
            configs.each(function(i) {
                var ruleData = $(this).data('data');
                var rule = $(this);
                console.error("Item Disabled:" + rule.find(".node-input-name-value").attr("disabled"));
                if (!rule.find(".node-input-name-value").attr("disabled")) {
                  	var configName = rule.find(".node-input-name-value").val();
                 	var configPath = rule.find(".node-input-path-value").val();
                  	var r = {cn:configName, cp:configPath};
                  	node.configs.push(r);
                }
            });
        },
        oneditresize: function(size) {
            var rows = $("#dialog-form>div:not(.node-input-rule-container-row)");
            var height = size.height;
            for (var i=0;i<rows.size();i++) {
                height -= $(rows[i]).outerHeight(true);
            }
            var editorRow = $("#dialog-form>div.node-input-rule-container-row");
            height -= (parseInt(editorRow.css("marginTop"))+parseInt(editorRow.css("marginBottom")));
            $("#node-input-rule-container").editableList('height',height);
        },
        oneditdelete:function(){

        }
    });
</script>

